import { getUserAddressList } from '../../services/user';
var app = getApp()
Page({
  data: {
    chooseAddressID: 0,
    addressList: [],
    type: 0
  },
  onLoad(event) {
    if (Object.keys(event).length > 0) {
      this.setData({ type: event.type })
    }


  },
  onShow() {
    my.setNavigationBar({
      title: '我的地址',
      backgroundColor: '#4BA3F9'
    })
    this.setData({ chooseAddressID: app.globalData.chooseAddressID })
    this.getUserAddressList()
  },
  add() {
    my.navigateTo({
      url: '/pages/addAddress/addAddress',
    })
  },
  chooseClick() {
    if (this.data.chooseAddressID <= 0) {
      my.showToast({
        type: 'fail',
        content: '请选择地址',
        duration: 2000,
      });
      return
    }
    app.globalData.chooseAddressID = this.data.chooseAddressID

    my.navigateBack()
  },
  onChoose(event) {
    console.log(event)
    this.setData({
      chooseAddressID: event.target.dataset.id
    });
  },
  compile(event) {
    //  带id为修改，不带id为新增

    my.navigateTo({
      url: '/pages/addAddress/addAddress?addressInfo=' + JSON.stringify(event.target.dataset.dataInfo),
    })
  },
  getUserAddressList() {
    getUserAddressList({}).then((res) => {
      if (res.code == "000000") {
        this.setData({
          addressList: res.data
        });

      }
    })
  }
});
